// 初始化任务数组
let tasks = [];

function addTask() {
    // 获取输入框的值
    const taskInput = document.getElementById('taskInput');
    const taskText = taskInput.value.trim();

    // 如果输入框为空，则不添加任务
    if (taskText === '') {
        console.log('请输入任务内容');
        return;
    }

    // 创建任务对象
    const task = {
        id: Date.now(), // 使用当前时间戳作为唯一ID
        text: taskText,
        completed: false,
        priority: document.getElementById("prioritySelect").value // 获取优先级
    };

    // 将任务添加到数组中
    tasks.push(task);

    // 清空输入框
    taskInput.value = '';

    // 更新任务列表
    renderTasks();
}

function toggleTask(id) {
    tasks = tasks.map(task => 
        task.id === id ? {...task, completed: !task.completed} : task
    );
    renderTasks();
}

function deleteTask(id) {
    tasks = tasks.filter(task => task.id !== id);
    renderTasks();
}

function renderTasks() {
    const taskList = document.getElementById('todo_list');
    taskList.innerHTML = '';
    
    tasks.forEach(task => {
        const li = document.createElement('li');
        li.className = task.completed ? 'completed' : '';
        li.innerHTML = `
            <span onclick="toggleTask(${task.id})">
                <div class="priority ${task.priority}-priority"></div>
                ${task.text}
            </span>
            <button class="deleteBtn" onclick="deleteTask(${task.id})">×</button>
        `;
        taskList.appendChild(li);
    });
}

// 初始化加载
renderTasks();